package cn.lsc.web.controller;

import cn.lsc.dto.CourseDto;
import cn.lsc.service.ICourseService;
import cn.lsc.domain.Course;
import cn.lsc.query.CourseQuery;
import cn.lsc.result.JSONResult;
import cn.lsc.result.PageList;
import cn.lsc.vo.CourseInfoVo;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    @RequestMapping(value="/onLineCourse/{courseId}",method= RequestMethod.POST)
    public JSONResult onLineCourse(@PathVariable("courseId") @Valid Long  courseId){
        courseService.onLineCourse(courseId);
        return JSONResult.success();
    }

    /**
    * 保存和修改公用的
    */
//    @RequestMapping(value="/save",method= RequestMethod.POST)
//    public JSONResult saveOrUpdate(@RequestBody Course course){
//        if(course.getId()!=null){
//            courseService.updateById(course);
//        }else{
//            courseService.insert(course);
//        }
//        return JSONResult.success();
//    }
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult save(@RequestBody @Valid CourseDto courseDto){
        courseService.save(courseDto);
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        page = courseService.selectPage(page);
        return JSONResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }
    @GetMapping("/detail/data/{courseId}")
    public JSONResult courseDetail(@PathVariable("courseId") Long courseId){
        CourseInfoVo vo = courseService.courseDetail(courseId);
        return JSONResult.success(vo);
    }
    @GetMapping("/getForUser/{mediaId}")
    public JSONResult getForUser(@PathVariable("mediaId") Long mediaId){
       String url= courseService.getForUser(mediaId);
        return JSONResult.success(url);
    }

}
